<!--
 * @Description: 打印组件
 * @Date: 2023-07-25 17:29:00
 * @FilePath: \hksj-vued:\03code\hk-marketing-crm\src\components\table\exportDialog.vue
-->
<template>
  <div class="print-container">
    <el-dialog v-model="state.isShowDialog" title="导出表格" width="40%">
      <el-form :model="form" label-width="120px" :rules="rules" ref="formRef">
        <el-form-item label="表格名称" prop="fileName">
          <el-input v-model="form.fileName" autocomplete="off" clearable />
        </el-form-item>
        <el-form-item label="表格开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="date"
            placeholder="请选择起始时间"
            :disabled-date="disabledDate"
            :shortcuts="shortcuts"
            style="width: 100%;"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="表格结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="date"
            placeholder="请选择结束时间"
            :disabled-date="disabledDate"
            :shortcuts="shortcuts"
            style="width: 100%;"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="onConfirm(formRef)">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="exportDialog">
import axiosRequest from '/@/utils/request';
import { FormInstance, FormRules } from 'element-plus';
import { reactive, ref } from 'vue';

const state: any = reactive({
	isShowDialog: false,
  exportUrl: '',
});

const form = reactive({
	fileName: "",
	startTime: "",
  endTime: "",
});
const formRef = ref<FormInstance>();
const rules = reactive<FormRules>({
  fileName: [
    { required: true, message: '请输入自定义的文件名', trigger: 'blur' },
    { min: 2, max: 50, message: '长度2到50', trigger: 'blur' },
  ],
  startTime: [
    {
      type: 'date',
      required: true,
      message: '请选择起始时间',
      trigger: 'change',
    },
  ],
  endTime: [
    {
      type: 'date',
      required: true,
      message: '请选择结束时间',
      trigger: 'change',
    },
  ],
});

const shortcuts = [
  {
    text: '今天',
    value: new Date(),
  },
  {
    text: '昨天',
    value: () => {
      const date = new Date()
      date.setTime(date.getTime() - 3600 * 1000 * 24)
      return date
    },
  },
  {
    text: '一周前',
    value: () => {
      const date = new Date()
      date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
      return date
    },
  },
]

const disabledDate = (time: Date) => {
  return time.getTime() > Date.now()
}

// 打开弹窗
const openDialog = (exportUrl:any) => {
  state.exportUrl = exportUrl;
	state.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	state.isShowDialog = false;
};

// 确认
const onConfirm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid:any, fields:any) => {
    if (valid) {
      const res = await axiosRequest({
				url: state.exportUrl,
				method: 'post',
				data: form,
        responseType: 'blob',
				headers:{ staticResource:true } // 资源标识
			});
      downloadSource(res);
      state.isShowDialog = false;
    } else {
      console.log('error submit!', fields)
    }
  })
}

// 下载流
const downloadSource = (res: any) => {
	let url =(window.URL || window.webkitURL).createObjectURL(new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' }));
  let link = document.createElement('a');
  link.style.display = 'none';
  link.href = url;
  link.setAttribute('download', `${form.fileName}-(${form.startTime}-${form.endTime})`);
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

defineExpose({
	openDialog,
	closeDialog,
});
</script>

<style scoped lang="scss">
.print-container{
  
}
</style>
